<#include "/common/common.ftl">
<@html>
<script type="text/javascript" src="${path}/js/dojo/dojo/inputForm.gzjs"></script>
<script type="text/javascript" src="${path}/js/dojo/dojo/checkboxTree.gzjs"></script>
<script type="text/javascript">
	//dojo.require("dijit.dijit");
	//dojo.require("dijit.form.Form");
	//dojo.require("dijit.form.ValidationTextBox");
	//dojo.require("dijit.form.SimpleTextarea");
	//dojo.require("dijit.Tree");
	//dojo.require("dojo.data.ItemFileWriteStore");
	//dojo.require("dijit.tree.TreeStoreModel");
	//dojo.require("dijit.Menu");
	//dojo.require("dijit.form.Button");
	//dojo.require("dijit.layout.ContentPane");
</script>
<div dojoType="dojo.data.ItemFileWriteStore" jsId="store"
     url="${path}/system/organization.loadTree.do"></div>
<div dojoType="dijit.tree.TreeStoreModel" jsId="model" childrenAttrs="subOrganization"
     store="store"></div>
<div dojoType="dijit.Tree" style="width:300px;margin-left:5px;" id="orgTree"
     jsId="orgTree" model="model" openOnClick="true">
</div>
<div dojoType="dijit.layout.ContentPane" jsId="detail"
     style="position:absolute;left:330px;top:2px;width:500px;height:90%">
</div>
<div dojoType="dijit.Menu" id="menu" targetNodeIds="orgTree" style="display: none;">
	<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="addOrg" iconClass="file_add"> 增加子机构
	</div>
	<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="editOrg" iconClass="file_edit"> 修改机构信息
	</div>
	<div dojoType="dijit.MenuItem" style="margin:2px;text-align:center;" onClick="deleteOrg" iconClass="file_delete">
		删除此机构
	</div>
</div>
</@html>
<script type="text/javascript">
	dojo.ready(function() {
		orgTree.getIconClass = function(item, opened) {
			if (store.isItem(item)) {
				var level = store.getValue(item, "level");
				/*var type = store.getValue(item,"type");
								 switch(type){
									 case "":
										 return "";
									 case "":
										 return "";
									 default :
										 return "";
								 }*/
				if (level == 0) {
					return "company";
				} else {
					return "department";
				}
			}
			return "";
		}
	})

	function btnAddClick() {
		if (dijit.byId("detailForm").validate()) {
			dojo.xhrPost({
						url:"${path}/system/organization.add.do",
						form:dojo.byId("detailForm"),
						handleAs:"json",
						load:function(json) {
							parent.Info("保存成功！");
							var selected = orgTree.get("selectedItem");
							model.newItem({id:json.id,name:json.name,level:json.level,type:json.type}, selected, 0);
							detail.set("content", "");
						}
					})
		}
	}

	function btnCloseClick() {
		detail.set("content", "");
	}

	function btnSaveClick() {
		if (dijit.byId("detailForm").validate()) {
			dojo.xhrPost({
						url:"${path}/system/organization.update.do",
						form:dojo.byId("detailForm"),
						handleAs:"json",
						load:function(json) {
							parent.Info("保存成功！");
							var selected = orgTree.get("selectedItem");
							store.setValue(selected, "name", json.name);
							detail.set("content", "");
						}
					})
		}
	}

	function editOrg() {
		var selected = orgTree.get("selectedItem");
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			detail.set("href", "${path}/system/organization.load.do?organization.id=" + id);
		}
	}

	function addOrg() {
		var selected = orgTree.get("selectedItem");
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			detail.set("href", "${path}/system/organization.load.do?parentId=" + id);
			//打开新窗口
			/*
					   parent.openDialog({
						   url:"${path}/system/organization.load.do?parentId="+id
		});
		*/
		}
	}

	function deleteOrg(ingoreSubOrg) {
		var selected = orgTree.get("selectedItem");
		var ingore = ingoreSubOrg || "0";
		if (store.isItem(selected)) {
			var id = store.getValue(selected, "id");
			dojo.xhrGet({
						url:"${path}/system/organization.delete.do?organization.id=" + id + "&ingoreSubOrg=" + ingore,
						handleAs:"json",
						load:function(message) {
							var type = 0;
							var msg = [];
							for (var i = 0; i < message.length; i++) {
								msg.push(message[i].text);
								//提示消息
								if (message[i].type == 1) {
									type = 1;
								}
								//警告消息
								if (message[i].type == 2) {
									type = 2;
								}
								//错误消息
								if (message[i].type == 3) {
									type = 3;
								}
							}
							if (type == 1) {
								parent.Info(msg.join("\n"), function() {
									store.deleteItem(selected);
								});
							}
							if (type == 2) {
								parent.Confirm({
											msg:msg.join("\n"),
											okCall:function() {
												deleteOrg(1);
											}
										});
							}
							if (type == 3) {
								parent.Error(msg.join("\n"));
							}
						}
					})
		}
	}
</script>